package com.example.martian.config.impl;

import com.example.martian.config.CacheService;
import com.example.martian.config.util.RedisUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CacheServiceImpl implements CacheService {
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    public RedisUtils getRedisUtils() {
        return new RedisUtils(redisTemplate);
    }

    /**
     * 获取数据
     */
    @SuppressWarnings("unchecked")
    public <T> T cacheResult(String key) {
        return (T) getRedisUtils().getDataFromCache(key);
    }

    /**
     * 清除缓存
     */
    public void cacheRemove(String key) {
        getRedisUtils().clearCache(key);
    }

    /**
     * 放入缓存
     */
    public <T> void cachePut(String key, T value) {
        getRedisUtils().setDataToCache(key, value);
    }

    /**
     * 放入缓存
     */
    public <T> void cachePut(String key, T value, long express) {
        getRedisUtils().setDataToCache(key, value, express);
    }

    /**
     * 模糊清除缓存
     */
    public void cachePrexRemove(String prex) {
        getRedisUtils().clearPrexCache(prex);
    }

    /**
     * 清除其他缓存库
     */
    public void cacheRemoveOtherDB(String key, int dbindex) {
        getRedisUtils().cacheRemoveOtherDB(key, dbindex);
    }
}
